
import type {UserInfo} from '../pinia/loginController'

export function formatLoginData(data:UserInfo[]){
        data= deepClone(data)
     return  data.reduce((perv:UserInfo[],cur,index,arr)=>{
            let arr1 = arr.filter(item=>item.pid === cur.id)
           if(arr1.length>0){
            cur.children = arr1
           }
           if(cur.pid===0){
            perv.push(cur)
           }
           return perv
        },[])

}
// 生成路由结构
export function routerToTree(dataList:UserInfo[]){
  return  dataList.map(function fn(item){
            let obj:any = {}
            if(item.children){
                obj.path=item.path;
                obj.name=item.name;
                obj.component = ()=>import(`../view/${item.name}.vue`)
                obj.children = item.children.map((item:any)=>fn(item))
                return obj;
            }else{
                obj.path = item.path;
                obj.name=item.name;
                obj.component = ()=>import(`../view/${item.name}.vue`)
                return obj 
            }
        })
}
//生成菜单结构
export function menuDataToTree(dataList:any[]){
    return   dataList.map(function fn(item){
        let obj:any = {}
        if(item.children){
            obj.name=item.name;
            obj.link=item.link;
            obj.title =item.title;
            obj.children = item.children.map((item:any)=>fn(item))
            return obj;
        }else{
            obj.name = item.name;
            obj.title= item.title;
            obj.link=item.link;
            return obj 
        }
    })
}
type NewData = Record<string|number,any>
export function deepClone(data:any){
    if(typeof data !=='object'||data===null){
        return data
    }
    let newData:NewData = Array.isArray(data)?[]:{}
     for(var k in data){
            if( data.hasOwnProperty(k)){
              newData[k] = deepClone(data[k])
            }
          }
    return newData
}
